package hn.sigit.logic.workflow;

import hn.sigit.logic.bpm.AttachedFileData;
import hn.sigit.logic.general.GeneralHelper;
import hn.sigit.logic.general.ResourceBundleHelper;
import hn.sigit.logic.general.SpatialAnalysisHelper;
import hn.sigit.logic.general.impl.ResourceBundleHelperImpl;
import hn.sigit.logic.viewer.InteractiveViewerHelper;
import hn.sigit.model.ladm.administrative.LA_AdministrativeSource;
import hn.sigit.util.AppOptions;

import java.util.ArrayList;
import java.util.List;

import org.jboss.seam.annotations.In;
import org.jboss.seam.bpm.Actor;
import org.jboss.seam.bpm.PooledTaskInstanceList;
import org.jboss.seam.bpm.TaskInstanceList;
import org.jbpm.taskmgmt.exe.TaskInstance;
import org.richfaces.event.UploadEvent;
import org.richfaces.model.UploadItem;

public abstract class WorkflowHelperBase implements WorkflowHelper {
	protected List<AttachedFileData> attachedFiles;
	protected List<LA_AdministrativeSource> fileSources;
	protected String editMode = "0"; //0: agregando, 1: editando
	protected AttachedFileData selectedFile;
	protected boolean attachInfo = true;
	protected int currentRow;

	@In
	protected AppOptions appOptions;

	@In
	protected Actor actor;
	
	@In(required=false)
	protected String loggedUserFullName;
	
	@In
	protected InteractiveViewerHelper interactiveViewerHelper;
	
	@In
	protected SpatialAnalysisHelper spatialAnalysisHelper;
	
	@In
	protected GeneralHelper generalHelper;
	
	@In
	private ResourceBundleHelper resBundle;


	@Override
	public List<AttachedFileData> getAttachedFiles() {
		return attachedFiles;
	}
	@Override
	public void setAttachedFiles(List<AttachedFileData> attachedFiles) {
		this.attachedFiles = attachedFiles;
	}
	
	@Override
	public abstract List<LA_AdministrativeSource> getFileSources();


	@Override
	public String getEditMode() {
		return editMode;
	}
	@Override
	public void setEditMode(String editMode) {
		this.editMode = editMode;
	}
	
	@Override
	public AttachedFileData getSelectedFile() {
		return selectedFile;
	}
	@Override
	public void setSelectedFile(AttachedFileData selectedFile) {
		this.selectedFile = selectedFile;
	}
	
	@Override
	public boolean isAttachInfo() {
		return attachInfo;
	}
	@Override
	public void setAttachInfo(boolean attachInfo) {
		this.attachInfo = attachInfo;
	}
	
	@Override
	public int getCurrentRow() {
		return currentRow;
	}
	@Override
	public void setCurrentRow(int currentRow) {
		this.currentRow = currentRow;
	}
		
	@Override
	public abstract String hasAttachedFiles();
	@Override
	public abstract void acceptSelectedFile();

	@Override
	public void deleteSelectedFile() {
		attachedFiles.remove(currentRow);
	}

	@Override
	public void attachFileListener(UploadEvent event) {
		UploadItem item = event.getUploadItem();
		if (null == attachedFiles)
			attachedFiles = new ArrayList<AttachedFileData>();
		
		AttachedFileData afd = new AttachedFileData();
		afd.setFileName(item.getFileName());
		afd.setFile(item.getFile());
		
		setSelectedFile(afd);
		
		item.getFile().deleteOnExit();
	}
	
	@Override
	public abstract String beginProcess();
	@Override
	public abstract String startTask();
	@Override
	public abstract String endTask();
	

	protected ResourceBundleHelper getResBundle() {
		if (resBundle == null) {
			resBundle = new ResourceBundleHelperImpl();
		}
		return resBundle;
	}

	protected List<TaskInstance> getTaskInstanceList(String processDefinition) {
		List<TaskInstance> taskList = new ArrayList<TaskInstance>();
		TaskInstanceList taskInstanceList = new TaskInstanceList();
		for (TaskInstance task : taskInstanceList.getTaskInstanceList())
			if (task.getProcessInstance().getProcessDefinition().getName().equals(processDefinition))
				taskList.add(task);
		
		return taskList;
	}
	
	protected List<TaskInstance> getPooledTaskInstanceList(String processDefinition) {
		List<TaskInstance> taskList = new ArrayList<TaskInstance>();
		PooledTaskInstanceList taskInstanceList = new PooledTaskInstanceList();
		for (TaskInstance task : taskInstanceList.getPooledTaskInstanceList())
			if (task.getProcessInstance().getProcessDefinition().getName().equals(processDefinition))
				taskList.add(task);
		
		return taskList;
	}
	
	@Override
	public abstract List<TaskInstance> getTaskInstanceList();
	@Override
	public abstract List<TaskInstance> getPooledTaskInstanceList();

}
